package com.codingvip.LRU;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author Tony.Han
 * @Created at 2020/4/9 2:19 下午
 * Desc:
 */
public class LRUCache  extends LinkedHashMap<Integer, Integer> {
        private int capacity;

        public LRUCache(int capacity) {
            super(capacity, 0.75F, true);
            this.capacity = capacity;
        }

        public int get(int key) {
            return super.getOrDefault(key, -1);
        }

        public void put(int key, int value) {
            super.put(key, value);
        }

        @Override
        protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
            return size() > capacity;
        }

    public static void main(String[] args) {
        LRUCache lruCache = new LRUCache(4);
        System.out.println(lruCache.get(1));
        lruCache.put(1, 1);
        lruCache.put(2, 2);
        lruCache.put(3, 3);

        lruCache.put(4, 4);
        lruCache.put(5, 5);
        System.out.println(lruCache.size());

    }

}
